package logic

import (
	"context"
	"fmt"
	"zhaowei/baidurpc/baidurpc"
	"zhaowei/baidurpc/internal/svc"
	"zhaowei/models/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type PostBarAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPostBarAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PostBarAddLogic {
	return &PostBarAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PostBarAddLogic) PostBarAdd(in *baidurpc.PostBarAddRequest) (*baidurpc.PostBarAddResponse, error) {
	// todo: add your logic here and delete this line
	var postb mysql.PostBar
	mysql.DB.Debug().Where("types_id = ?", in.TypesId).Find(&postb)
	if postb.TypesId != int(in.TypesId) {
		return nil, fmt.Errorf("此论坛所属分类不存在")
	}
	postb.Name = in.Name
	postb.Number = int(in.Number)
	postb.Image = in.Image
	postb.TypesId = int(in.TypesId)
	mysql.DB.Debug().Create(&postb)
	return &baidurpc.PostBarAddResponse{
		Id: int64(postb.ID),
	}, nil
}
